package com.shen.mianshi.xinde;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
/**
 * 生产者
 * @author Administrator
 *
 */
public class Provider implements Runnable{
	
	//共享缓存区
	private BlockingQueue<Message> queue;
	//多线程间是否启动变量，有强制从主内存中刷新的功能。即时返回线程的状态
	private volatile boolean isRunning = true;
	//id生成器
	private static AtomicInteger count = new AtomicInteger();
	
	public Provider(BlockingQueue queue){
		this.queue = queue;
	}

	public void run() {
		while(isRunning){
			try {
				//获取的数据进行累计...
				int id = count.incrementAndGet();
				
				Message msg = new Message();
				msg.setContent("内容"+id);
				msg.setId(id);
				msg.setType(id%2+"");
				if(!this.queue.offer(msg, 2, TimeUnit.SECONDS)){
					System.out.println("提交缓冲区数据失败....");
				}
				
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
	public void stop(){
		this.isRunning = false;
	}
	
}
